package com.homwee.factory.pageData;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;

import com.homwee.factory.FactoryActivity;
import com.homwee.factory.FactoryDialog;
import com.homwee.factory.R;
import com.homwee.listMenu.util.MenuItemData;
import com.homwee.factory.*;
import com.homwee.factory.util.Contants;

public class PageData7 extends PageData{
	private Context context;
	private final String TAG="CHFactory.PageData7";
	private String[] mMemcEffectArray = {
			"Off", "Low", "Middle", "High"
	};
	
	public PageData7(Context context,FactoryActivity mFactoryActivity){
		super(mFactoryActivity);
		this.context = context;
	}
	
	public List<MenuItemData> LoadPageData(){
		mListItemData.clear();
		String factorySettingStr = mFactoryActivity.getResources().getString(R.string.factory_setting);
		String mmodeStr=mFactoryActivity.getResources().getString(R.string.factory_mmode);
		MenuItemData facSetting =new MenuItemData(factorySettingStr);
		MenuItemData mMode=new MenuItemData(mmodeStr);
		facSetting.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				//mFactoryActivity.showMenu(false);
				Log.v("tv","systemInit");
				
          	  /* FactoryToast toast = new FactoryToast(context);
  		       toast.setDuration(5000);
  		       toast.setToastIcon(R.drawable.pic_eye);
  		       toast.setMessage(context.getString(R.string.factory_system_reset));
  		       toast.getTextView().getBackground().setAlpha(200);
  		       toast.show();*/
  		       
  		       FactoryDialog mDialog=new FactoryDialog(context,"FACTORY_RESET");
  		       mDialog.getWindow().setType(
						WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  		       mDialog.show();
          	  /* new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
	    		       factorySetting.systemInit();
					}
					}, 200);*/
          	   
          	   
          	   
			}
		});	
		
		mMode.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			@Override
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				if (FactoryKeyReciver.isFactoryMMode(context)) {
                    FactoryKeyReciver.setFactoryMMode(context, 0);
                    factorySetting.setPowerMode(0);
                    Intent intentForBarCode = new Intent();
                    intentForBarCode
                            .setAction("com.homwee.action.STOP_FACTORY_SERVICE");
                    context.sendBroadcast(intentForBarCode);
                }
				else {
                    FactoryKeyReciver.setFactoryMMode(context, 1);
                    factorySetting.setPowerMode(2);
                    /*Intent intentForBarServer = new Intent(
                            "FactoryModeService");
                    intentForBarServer.setPackage("com.homwee.factory");
                    mContext.startService(intentForBarServer);*/
					Intent intentForBarServer = new Intent(context,FactoryModeService.class);
                    context.startService(intentForBarServer);
                }

                FactoryModeMenu.refresh(context);
			}
		});

		MenuItemData mMemc = new MenuItemData(0, "MEMC Effect", 0,
				mMemcEffectArray.length - 1, 0, mMemcEffectArray, 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		try{
			//InitValue
			int index=factorySetting.getMemcIndex();
			mMemc.mInitValue = index;
			Log.d(TAG,"getMemcIndex:"+index);
		}catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		mMemc.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v(TAG, "change memc value:" + value);

				try {
					factorySetting.setMemcIndex(value);
					Settings.Global.putInt(context.getContentResolver(), "tv_picture_advance_video_mjc_effect", value);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		String memcReset="MEMC Restore To Default";
		MenuItemData mMemcReset=new MenuItemData(memcReset);
		mMemcReset.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			@Override
			public void onEnterRightKey() {

				mMemc.mInitValue = factorySetting.resetMemc();
				Settings.Global.putInt(context.getContentResolver(), "tv_picture_advance_video_mjc_effect", mMemc.mInitValue);
			}
		});

		String dataIndexInfo="Data Index Info";
		MenuItemData mDataIndexInfo=new MenuItemData(dataIndexInfo);
		mDataIndexInfo.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			@Override
			public void onEnterRightKey() {
				Intent intentForDataIndexInfo = new Intent();
				intentForDataIndexInfo.setClass(context, FactoryDataIndexInfoActivity.class);
				intentForDataIndexInfo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intentForDataIndexInfo);
			}
		});
		String autoEnterFactortyStr="Auto Enter Factory";
		MenuItemData mAutoEnterFactory=new MenuItemData(0,autoEnterFactortyStr,0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mAutoEnterFactory.mInitValue=Integer.parseInt(factorySetting.getAutoEnterFactory());
		mAutoEnterFactory.setOnValueChange(new MenuItemData.onValueChange() {
			@Override
			public void valueChange(int value) {
				factorySetting.setAutoEnterFactory(Integer.toString(value));
			}
		});

		mListItemData.add(facSetting);
		mListItemData.add(mMode);
		mListItemData.add(mMemc);
		mListItemData.add(mMemcReset);
		mListItemData.add(mDataIndexInfo);
		mListItemData.add(mAutoEnterFactory);
		
		return mListItemData;
	}
}
